package com.lenovo.leos.filebrowser.engine.operation;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.lenovo.leos.filebrowser.R;
import com.lenovo.leos.filebrowser.engine.util.FileUtil;
import com.lenovo.leos.filebrowser.engine.util.Log;
import com.lenovo.leos.filebrowser.ui.BaseActivity;

public class FileShare {
	private static final String TAG = "FileShare";
	
	// msg id
	public static final int MSG_RES_OK = R.string.msg_share_res_ok;
	public static final int MSG_RES_FAILED = R.string.msg_share_res_failed;
	
	private static BaseActivity sActivity = null; 
	
	public static void send(BaseActivity activity, File file) {
		Log.i(TAG, "==> send");

		if (null == activity 
				|| null == file || !file.exists()) {
			Log.e(TAG, "invalid parameter in send");
			return;
		}else {
			sActivity = activity;
			
			checkThenSend(activity, file);
		}
	}
	
	private static void checkThenSend(BaseActivity activity, final File file) {
		if (file.isDirectory()){
			//[improve] 
			Toast.makeText(sActivity, "can not share a folder", Toast.LENGTH_SHORT).show();
			return;
		}else
			sendFile(activity,file);
	}
	
	private static void sendFile(BaseActivity activity,final File file) {
		
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.addCategory(Intent.CATEGORY_DEFAULT);

		intent.setType(FileUtil.getMimeTypeOfFile(sActivity, file));
		intent.putExtra(Intent.EXTRA_STREAM, FileUtil.getUri(file));
//		intent.putExtra(Intent.EXTRA_TEXT, FileUtil.getFile(file)); //add 2012-12-16 by xieqm

		String title = activity.getText(R.string.send) + file.getName();

		try {
			sActivity.startActivity(Intent.createChooser(intent, title));
			sActivity.sendEmptyMsg(MSG_RES_OK);
		} catch (android.content.ActivityNotFoundException ex) {
			Toast.makeText(sActivity, R.string.pleaseselectfile,
					Toast.LENGTH_SHORT).show();
			sActivity.sendEmptyMsg(MSG_RES_FAILED);
		}				
	}
	
}